package com.css.fxfzdzzh.modules.surfaceRupture.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.modules.hypergraph.service.HypergraphService;
import com.css.fxfzdzzh.modules.surfaceRupture.entity.SurfaceRupture;
import com.css.fxfzdzzh.modules.surfaceRupture.param.PageQueryParam;
import com.css.fxfzdzzh.modules.surfaceRupture.service.SurfaceRuptureService;
import com.css.fxfzdzzh.modules.surfaceRupture.vo.DeleteVO;
import com.css.fxfzdzzh.modules.surfaceRupture.vo.ParamVO;
import com.css.fxfzdzzh.modules.surfaceRupture.vo.SurfaceRuptureVO;
import com.css.fxfzdzzh.util.ResponseCreator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年04月08日  13:49
 */
@RestController
@RequestMapping("/surfaceRupture")
@Validated
public class SurfaceRuptureController {
    @Resource
    SurfaceRuptureService surfaceRuptureService;
    @Resource
    HypergraphService hypergraphService;

    /**
     * 保存地表破裂带记录
     */
    @PostMapping("/saveSurfaceRupture")
    public RestResponse saveSurfaceRupture( @RequestBody SurfaceRuptureVO vo) throws Exception {
        String id = surfaceRuptureService.saveSurfaceRupture(vo);
        return ResponseCreator.createSuccessResponse(id,"保存成功");
    }
    /**
     * 地表破裂带评估
     */
    @PostMapping("/evaluate")
    public RestResponse evaluate( @RequestBody Map<String,String> map) throws Exception {
        SurfaceRupture surfaceRupture = surfaceRuptureService.evaluate(map.get("dsrId"));
        //刷新指定超图工作空间数据源数据集
        hypergraphService.getWorkspaceReload("dzzh_surface_rupture");
        hypergraphService.getWorkspaceReload("dzzh_surface_rupture_displacement");
        hypergraphService.getWorkspaceReload("dzzh_surface_rupture_fault");
        hypergraphService.getWorkspaceReload("dzzh_seismogenic_structure_stimate");
        return ResponseCreator.createSuccessResponse(surfaceRupture);
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public RestResponse page(PageQueryParam pageQueryParam) throws Exception {
        Map<String,Object> page = surfaceRuptureService.page(pageQueryParam);
        return ResponseCreator.createSuccessResponse(page);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public RestResponse delete( @RequestBody DeleteVO vo) throws Exception {
        if(vo.getIdList().size()<1){
            throw new RuntimeException("请选择要删除的数据");
        }
        surfaceRuptureService.delete(vo.getIdList());
        return ResponseCreator.createSuccessResponse("删除成功");
    }

    /**
     * 评估记录是否生成过评估结果
     */
    @GetMapping("/hasEvaluateResult")
    public RestResponse hasEvaluateResult( String dsrId) throws Exception {
        String msg = surfaceRuptureService.hasEvaluateResult(dsrId);
        if(StringUtils.isNotBlank(msg)){
            return ResponseCreator.createFailResponse(msg);
        }
        return ResponseCreator.createSuccessResponse("");
    }

    /** 
     * 保存地表破裂带评估参数
     */
    @PostMapping("/saveParam")
    public RestResponse saveParam(@RequestBody ParamVO paramVO) throws Exception{
        surfaceRuptureService.saveParam(paramVO);
        return ResponseCreator.createSuccessResponse(" 保存成功！");
    }
    /**
     * 获取地表破裂带评估参数
     */
    @GetMapping("/getParam")
    public RestResponse getParam() throws Exception{
        ParamVO param = surfaceRuptureService.getParam();
        return ResponseCreator.createSuccessResponse(param);
    }


    /**
     * 地质灾害   上传历史地表破裂带
     */
    @PostMapping("uploadExcel")
    public RestResponse uploadExcel(@RequestParam MultipartFile file){
        try {
            if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
                return RestResponse.fail("文件格式不支持!");
            }
            return surfaceRuptureService.uploadSingleBvInformation(file);
        } catch (Exception e) {
            return RestResponse.fail("导入失败!");
        }
    }
    

}
